﻿using System;
using System.Xml.Serialization;
using System.IO;

namespace TaskBoardProject
{
    /// <summary>
    /// XML Serialization for TaskBoard(collection of tasks)
    /// </summary>
    public static class TaskBoardXmlSerialization
    {
        /// <summary>
        /// Serialize
        /// </summary>
        /// <param name="fileName">Name of file</param>
        /// <param name="taskBoard">Represents an instance of Task, that will be saved</param>
        public static void SaveXmlDocument(string fileName, TaskBoard taskBoard)
        {
            XmlSerializer formatter = new XmlSerializer(typeof(TaskBoard));

            using (FileStream fileStream = new FileStream(fileName, FileMode.OpenOrCreate))
            {
                formatter.Serialize(fileStream, taskBoard);
            }
        }
        
       /// <summary>
        /// Deserialize
       /// </summary>
        /// <param name="fileName">Name of file</param>
        /// <returns>Represents an instance of Task, that will take new value</returns>
        public static TaskBoard LoadXmlDocument(string fileName)
        {
            XmlSerializer formatter = new XmlSerializer(typeof(TaskBoard));

            try
            {
                using (FileStream fStream = new FileStream(fileName, FileMode.Open))
                {
                    return (TaskBoard)formatter.Deserialize(fStream);
                }
            }
            catch (FileNotFoundException)
            {
                Console.WriteLine("File not found!");
                return new TaskBoard();
            }
        }
    }
}
